<template>
  <div>
    <div>
      <el-input
        class="addPosInput"
        placeholder="添加职位"
        suffix-icon="el-icon-plus"
        v-model="pos.name"
        @keydown.enter.native="addPosition"
      >
      </el-input>
      <el-button icon="el-icon-plus" type="primary" @click="addPosition"
        >添加</el-button
      >
    </div>

    <div class="posManagementMain">
      <el-table
        :data="positions"
        stripe
        border
        style="width: 70%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="编号" width="60"> </el-table-column>
        <el-table-column prop="name" label="职位" width="130">
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="220">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="showEditView(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
        <!-- <el-table-column prop="enabled" label="是否启用" width="120">
        </el-table-column> -->
      </el-table>

      <el-button
        type="danger"
        style="margin-top: 10px"
        :disabled="this.multipleSelection.length == 0"
        @click="deleteMany"
        >批量删除</el-button
      >

      <el-dialog title="编辑职位" :visible.sync="dialogVisible" width="30%">
        <div>
          <el-tag>职位名称</el-tag>
          <el-input v-model="updatePos.name" class="updatePosInput"></el-input>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="doUpdate">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "PosManagement",
  data() {
    return {
      pos: {
        name: "",
      },
      positions: [],
      dialogVisible: false,
      updatePos: {
        name: "",
      },
      multipleSelection: [],
    };
  },

  mounted() {
    this.initPositions();
  },

  methods: {
    deleteMany() {
      this.$confirm(
        "确定删除" + this.multipleSelection.length + "条职位信息吗?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let ids = "?";
          this.multipleSelection.forEach((item) => {
            ids += "ids=" + item.id + "&";
          });
          this.deleteRequest("/system/basic/pos/" + ids).then((resp) => {
            if (resp) {
              this.initPositions();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    doUpdate() {
      this.putRequest("/system/basic/pos/", this.updatePos).then((resp) => {
        if (resp) {
          this.initPositions();
          this.dialogVisible = false;
        }
      });
    },

    addPosition() {
      if (this.pos.name) {
        this.postRequest("/system/basic/pos/", this.pos).then((resp) => {
          if (resp) {
            this.initPositions();
            this.pos.name = "";
          }
        });
      } else {
        this.$message.error("职位不能为空");
      }
    },
    showEditView(index, data) {
      Object.assign(this.updatePos, data);
      this.updatePos.createDate = "";
      this.dialogVisible = true;
    },
    handleDelete(index, data) {
      this.$confirm("确定删除" + data.name + "职位吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteRequest("/system/basic/pos/" + data.id).then((resp) => {
            if (resp) {
              this.initPositions();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    initPositions() {
      this.getRequest("/system/basic/pos/").then((resp) => {
        if (resp) {
          this.positions = resp;
        }
      });
    },
  },
};
</script>
<style>
.addPosInput {
  width: 300px;
  margin-right: 10px;
}

.posManagementMain {
  margin-top: 15px;
}

.updatePosInput {
  width: 200px;
  margin-left: 10px;
}
</style>